package com.factory.iamp.dao;

import com.factory.iamp.pojo.TableTemplateHeader;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TableTemplateHeaderMapper {
    String base_table = "(select * from table_template_header where lid = #{lid}) as t1";

    @Select("select count(oid) from " + base_table)
    int Count(@Param("lid") int lid);

    @Select("select count(oid) from " + base_table + " where name like #{name}")
    int findByNameCount(@Param("name") String name, @Param("lid") int lid);

    @Select("select * from " + base_table + " limit #{offset}, #{limit}")
    List<TableTemplateHeader> ListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("lid") int lid);

    @Select("select * from " + base_table + " where name like #{name} limit #{offset}, #{limit}")
    List<TableTemplateHeader> findByNameListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("name") String name, @Param("lid") int lid
    );

    @Select("select * from " + base_table + " where oid = #{oid} limit 1")
    TableTemplateHeader findById(@Param("oid") int oid, @Param("lid") int lid);

    @Select("select * from " + base_table + " where name = #{name} limit 1")
    TableTemplateHeader findByName(@Param("name") String name, @Param("lid") int lid);

    @Insert("insert into table_template_header(lid, name, format, file_path, `json`, `defaultValue`, created_at) " +
            "values (#{lid}, #{name}, #{format}, #{file_path}, #{json}, #{defaultValue}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "oid", keyColumn = "oid")
    void addTableTemplateHeader(TableTemplateHeader tableTemplateHeader);

    @Update("update table_template_header set `json` = #{json} where oid = #{oid}")
    void setJson(@Param("oid") int oid, @Param("json") String json);

    @Delete("delete from table_template_header where oid = #{oid}")
    void deleteById(@Param("oid") int oid);
}
